package com.pengtu.web.asset.equipment;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.entity.asset.equipment.EquipInformation;
import com.pengtu.entity.asset.equipment.EquipMaintenance;
import com.pengtu.service.asset.EquipmentManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;
@Namespace("/asset/equipment")
@Result(name=BaseActionSupport.RELOAD,location="maintenance.action" ,type="redirect" )
public class MaintenanceAction extends BaseActionSupport<EquipMaintenance> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private EquipmentManager equipManager;

	private String id;
	private EquipMaintenance equipMaintenance;
	private List<EquipMaintenance> maintenanceList ;
	private Page<EquipMaintenance> page = new Page<EquipMaintenance>(10);
	private String equipId ;
	private HttpSession session = Struts2Utils.getSession();
	
	
	public void setEquipId(String equipId) {
		this.equipId = equipId;
	}

	public String getEquipId() {
		return equipId;
	}

	public Page<EquipMaintenance> getPage() {
		return page;
	}

	public List<EquipMaintenance> getMaintenanceList() {
		return maintenanceList;
	}

	@Autowired
	public void setEquipManager(EquipmentManager equipManager) {
		this.equipManager = equipManager;
	}

	public void setId(String id) {
		this.id = id;
	}


	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		if(StringUtils.isEmpty(equipId))equipId=(String) session.getAttribute("equipId");
		page = equipManager.findFuzzyEquipMaintenance(page);
		if(StringUtils.isNotEmpty(equipId)){
		     maintenanceList = equipManager.findEquipMaintenanceList("equipInformation.id", equipId);
			System.out.println(maintenanceList.size());
		     if(maintenanceList.size()>0){
				page.setResult(maintenanceList);
				page.setTotalCount(maintenanceList.size());
			}
		}
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return list();
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		if(StringUtils.isNotEmpty(equipId)){
			EquipInformation ekInformation = equipManager.getProperty("id", equipId);
			equipMaintenance.setEquipInformation(ekInformation);
		}
		equipManager.saveOrUpdateMaintenance(equipMaintenance);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage(mes+"设备维修记录");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		equipManager.deleteMaintenance(id);
		addActionMessage("删除设备维修记录");
		return list();
	}

	@Override
	protected void prepareModel() throws Exception {
			session.setAttribute("equipId", equipId);
			if(StringUtils.isNotEmpty(id)){
				equipMaintenance = equipManager.findPropertyMaintenance("id",id);
			}else{
				equipMaintenance = new EquipMaintenance();
				}
	}

	@Override
	public EquipMaintenance getModel() {
		// TODO Auto-generated method stub
		return equipMaintenance;
	}

}
